$NG.AllReady(function (page, { useDataIndexChange, useUpdateRow,useValuesChange , useUpdateRows }) {
    const mstform = $NG.getCmpApi("p_form_telephone_plan_m")
    const grid = $NG.getCmpApi("p_form_telephone_plan_d")
    let cityMap = {}
    $NG.execServer("getQweQwe", {}, function(res){
        console.log(res);
        console.log('测试', JSON.parse(res.data)[0]);
    })
    $NG.updateUI(updater => {
        console.log(updater.editGrid.p_form_telephone_plan_d.u_telephine);
        updater.editGrid.p_form_telephone_plan_d.u_telephine.setProps((p)=>{
            return {
                ...p, editor: {
                  ...p.editor,
                  onDoubleClick(){
                    console.log('双击成功!!!!!');
                  }
                }
            }
        })
    })
    if(page.oType = 'add'){
        $NG.execServer("getInDate",{},(res)=>{
            const _data = JSON.parse(res.data)[0].extendObjects
            $NG.updateState((updater)=>{
                updater.data.p_form_telephone_plan_m.setProps({
                    phid_cycle:{
                        label:_data.bill_name,
                        value:_data.phid
                    }
                })
            })
        })
        
    }
  
    //监听事业部
    useValuesChange(({args})=>{
        console.log(`事业部监听`, args[0]);
        getGxrInfo()
        getAllGxr()
        proDFilter()
        if (page.oType = 'add') {
            home = args[0].cur_orgid.label
            setCurOrgid()
        }
  
    },"p_form_telephone_plan_m.cur_orgid")
  
  setTimeout(() => {
        console.log(`============`, mstform.getItem("phid_cycle").getText());
    }, 2000)
    //设置单据名称
    function setCurOrgid() {
        if (!mstform.getItem("cur_orgid").getValue() || !mstform.getItem("bill_dt").getValue()) return
        $NG.updateState((updater) => {
            updater.data.p_form_telephone_plan_m.setProps({
                bill_name: `${home}事业部的${mstform.getItem("phid_cycle").getText()}电话回访计划`
            })
        })
    }
    useUpdateRows(({args})=>{
        console.log(`监听表格行为`);
        const row = args[0]
        let lock = false
        row.forEach(item => {
            if(item.u_visit_way !== '电话回访'){
                item.u_visit_way = "电话回访"
                lock = true
            } 
            if(item.u_gxrzt !=='1'){
                item.u_gxrzt = 1
            }
        })
        if(lock){
            grid.refreshView()
        }
    }, "p_form_telephone_plan_d")

    useDataIndexChange(({args})=>{
        getPtInfoByPhid(args[0].phid_pc,(datas)=>{
            args[0].u_pc_type = datas.type_phid,
            args[0].u_pc_type_EXName = datas.type_name
        grid.refreshView()
    })
    useUpdateRows(({args})=>{
        console.log(`进行中`);
        
    },"p_form_telephone_plan_d")
    },"phid_pc")
    // 获取干系人信息
    async function getGxrInfo(){
        grid.clearRows().then(()=>{
            $NG.execServer("getAllFilterGxr", {phid_org:mstform.getItem("cur_orgid").getValue()},async function(res){
                console.log(`获取干系人响应式数据`, res);
                console.log(`检查缓存`, cityMap);
                if(res.count == 0 ) return
                const datas = JSON.parse(res.data)
                console.log(datas);
                const _arr = []
                datas.forEach(item => {
                    console.log(`测试item`,item);
                    const _data = item.extendObjects
                    if(_data.gxr_where == '1')  {
                    _arr.push({
                        phid_pc:_data.phid_pc,
                        phid_pc_EXName:_data.bill_name,
                        u_provice:_data.pno||"",
                        u_provice_EXName:cityMap[_data.pno]||"",
                        u_city:_data.cno||"",
                        u_city_EXName:cityMap[_data.cno]||"",
                        u_khdwmc:_data.gxr_dwmc,
                        u_gxr:_data.u_gxr,
                        u_gxr_EXName:_data.u_gxr_xm,
                        u_gxrzt:_data.u_gxrzt,
                        u_telephine:_data.u_mobile_phone,
                        u_s_visit_time:_data.u_sj_visit_time,
                        u_schfqk:_data.schfqk,
                        u_remarks:_data.u_remarks
                    })
                    }
                    
                })
                await grid.addRows(_arr)
                grid.refreshView()
                setTimeout(()=>{
                const _arrs = grid.getRows()
                console.log(`检查数据信息`,_arrs);

                    _arrs.forEach(item=>{
                        getPtInfoByPhid(item.phid_pc,(data)=>{
                            console.log(`项目信息`,data);
                            item.u_pc_type = data.type_phid,
                            item.u_pc_type_EXName = data.type_name
                            grid.refreshView()
                        })
                    })
                },200)
            })
        })
    }
    // 获取省份信息
    function getAllGxr(){
        $NG.execServer("getAllCity", {}, function(res){
            getGxrInfo()
            const _data = JSON.parse(res.data)
            const _map = {}
            _data.forEach(item =>{
                const datas = item.extendObjects
                _map[datas.pid] = datas.pname
                _map[datas.cno] = datas.cname
            })
            cityMap = {..._map}
        })
    }
    // 根据事业部过滤项目
    function proDFilter(){
        const phid_pcc = $NG.getCmpApi('p_form_telephone_plan_m').getItem('cur_orgid').getValue();
        if(!phid_pcc){
          return 
        }
        $NG.updateUI(updater => updater.editGrid.p_form_telephone_plan_d.phid_pc.setProps((p) => {
          return {
            ...p, editor: {
              ...p.editor,
              clientSqlFilter: { phid_org: $NG.getCmpApi('p_form_telephone_plan_m').getItem('cur_orgid').getValue() },
            }
          };
        }));
    }
        //根据项目ID获取项目相关信息
function getPtInfoByPhid(phid,fun) {
    if (phid) {
      $NG.execServer("getPtInfoByPhid", { phid }, function (res) {
        console.log(`获取项目信息`, res,phid);
        const datas = JSON.parse(res.data)[0].extendObjects;
        console.log(datas);
        fun(datas)
      });
    }
  }
        // 格式化日期
function formatDate(dateStr, outputFormat) {
    const date = new Date(dateStr);

    if (isNaN(date)) {
        return "无效的日期字符串";
    }

    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    const hours = String(date.getHours()).padStart(2, '0');
    const minutes = String(date.getMinutes()).padStart(2, '0');

    let formattedDate;

    switch (outputFormat) {
        case "yyyy-mm-dd":
            formattedDate = `${year}-${month}-${day}`;
            break;
        case "yyyy-mm-dd HH:mm":
            formattedDate = `${year}-${month}-${day} ${hours}:${minutes}`;
            break;
        // 可以根据需要继续添加其他格式的处理逻辑
        default:
            formattedDate = "不支持的输出格式";
    }

    return formattedDate;
}
  });